package net.timeslicer.jabber;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.Occupant;

public final class JabberMessageThread implements Runnable {
	private static Logger log = Logger.getLogger(JabberMessageThread.class);
	private final Packet packet;
	
	public JabberMessageThread(final Packet packet) {
		this.packet = packet;
	}
	
	@Override
	public void run() {
		final Occupant occ = RoomUtil.getInstance().getSender(packet);
		
		final String sender = occ.getNick();
		final long timeStamp = System.currentTimeMillis();
		
		if(SpamMessageListener.previousSender.equals(sender)) {
			SpamMessageListener.count++;
			SpamMessageListener.totalTimestamps = SpamMessageListener.totalTimestamps +
				(timeStamp - SpamMessageListener.previousTimestamp);
			
		} else {
			SpamMessageListener.count = 0;
			SpamMessageListener.totalTimestamps = 0;
			
		}
		SpamMessageListener.previousSender = sender;
		SpamMessageListener.previousTimestamp = timeStamp;
		
		if(SpamMessageListener.count>=2) {
			if(SpamMessageListener.totalTimestamps < 1000) {
				RoomUtil.getInstance().kickSpammer(sender);
				return;
			}
		}
		
		final String xmlMessage = packet.toXML();
		if(StringUtils.countMatches(xmlMessage, "\u202E") >= 1) {
			log.info("detected backwards");
			RoomUtil.getInstance().registerBackwardsText(occ);
			return;
		}		
		int countOfFlashBangs = StringUtils.countMatches(xmlMessage, "<br/>");
		if(countOfFlashBangs > 1) {
			log.info("detected shiftenter by "+sender);
			if(countOfFlashBangs > 5) {
				ModThread.banOccupant(occ);
			} else {
				ModThread.kickOccupant(occ);
			}
			return;
		}	
		if(StringUtils.contains(xmlMessage, "sexy") && StringUtils.contains(xmlMessage, "pm me")) {
			ModThread.banOccupant(occ);
			return;
		}
		
		if(StringUtils.contains(xmlMessage, "join.seekcompanion.com")) {
			ModThread.banOccupant(occ);
			return;
		}
		if(occ.getJid().contains("mr_br00ks") || occ.getJid().contains("infinity_star") || occ.getRole().equals("moderator") || occ.getRole().equals("owner")) {
			processCommand(occ, xmlMessage);
			return;
		}		
	}

	private void processCommand(final Occupant occ, final String xmlMessage) {
		if(StringUtils.contains(xmlMessage,"b^guests")) {
				ModThread.cleanGuests();
				return;
		}		
		final String[] parts = StringUtils.split(xmlMessage,"^");
		try {
			if(parts.length == 1) return;
			
			final String[] nextParts = StringUtils.split(parts[1],"<");
			final String nick = nextParts[0];
			if(StringUtils.contains(xmlMessage,"um^")) {
				ModThread.unMuteGuest(nick);
				return;
			}	
			if(StringUtils.contains(xmlMessage,"uma^")) {
				ModThread.unMuteAll();
				return;
			}			
			if(StringUtils.contains(xmlMessage,"m^")) {
					ModThread.muteNick(nick);
					return;
			}
			if(StringUtils.contains(xmlMessage,"b^")) {
					ModThread.banByNick(nick);
					return;
			}
			if(StringUtils.contains(xmlMessage,"k^")) {
				ModThread.kickByNick(nick);
				return;
			}
			if(StringUtils.contains(xmlMessage, "bot: flag word ")) {
				log.info("word: "+nick);
				return;
			}
		} catch(Exception e) {
			log.error(e);
		}
	}
}
